﻿Public Class fZadaniPrijmy
    'check of possible no. of invoice duplicate
    Dim cisloExistuje As Boolean = False
    
    'saves text in cbox "cbox_Jmeno" input
    Dim cbText As String = ""
    Dim jm_Text As String = ""
    Dim pr_Text As String = ""
    Dim ad_Text As String = ""
    Dim ind_DD As Long = Nothing

    Private Sub fZadaniPrijmy_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Seznam_PrijemTypPlatbyTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemTypPlatby)
        Me.Seznam_PrijemZpusobProdejeTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemZpusobProdeje)
        Me.Seznam_vyrobkyTableAdapter1.Fill(Me.DtbDataSet1.seznam_vyrobky)
        Me.Tab_prijemTableAdapter1.Fill(Me.DtbDataSet1.tab_prijem)
        Me.Seznam_PrijemFakturyTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemFaktury)
        Me.Seznam_odberateleTableAdapter1.Fill(Me.DtbDataSet1.seznam_odberatele)
        '------------fill checkbox with names of products
        Try
            For i As Integer = 0 To Me.DtbDataSet1.seznam_vyrobky.Rows.Count() - 1
                Me.chboxFaktVyrobky.Items.Add(Me.DtbDataSet1.seznam_vyrobky.Rows.Item(i).Item(1))
            Next (i)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error loading list of products")
        End Try
        '-----------end of filling
        'clears text strip text in fMain
        My.Forms.fMain.lblTStrip1.Text = ""
        '---

        
    End Sub

    Private Sub btnPokracovat_Click(sender As System.Object, e As System.EventArgs) Handles btnPokracovat.Click
        Dim fill As String = ""
        If Me.tboxAdresa.Text.Length = 0 And Me.cboxTypProdeje.SelectedIndex <> 1 Then
            Me.tboxAdresa.BackColor = Color.Red
            fill = fill & Me.lblAdresa.Text & vbCrLf
        End If
        If Me.tboxCisloFaktury.Text.Length = 0 And Me.cboxTypProdeje.SelectedIndex <> 1 Then
            Me.tboxCisloFaktury.BackColor = Color.Red
            fill = fill & Me.lblCisloFaktury.Text & vbCrLf
        End If
        If Me.tboxJmeno.Text.Length = 0 And Me.cboxTypProdeje.SelectedIndex <> 1 Then
            Me.tboxJmeno.BackColor = Color.Red
            fill = fill & Me.lblJmeno.Text & vbCrLf
        Else
            Me.tboxPostovne.Text = "0"
        End If
        If Me.tboxPostovne.Text.Length = 0 And Me.cboxTypProdeje.SelectedIndex <> 1 Then
            Me.tboxPostovne.BackColor = Color.Red
            fill = fill & Me.lblPostovne.Text & vbCrLf
        End If
        If Me.tboxPrijmeni.Text.Length = 0 And Me.cboxTypProdeje.SelectedIndex <> 1 Then
            Me.tboxPrijmeni.BackColor = Color.Red
            fill = fill & Me.lblPrijmeni.Text & vbCrLf
        End If
        If Me.chboxFaktVyrobky.CheckedItems.Count() = 0 Then
            Me.chboxFaktVyrobky.BackColor = Color.Red
            fill = fill & Me.lblPocetRuznychVyrobku.Text & vbCrLf
        End If
        If fill.Length = 0 And Me.chboxFaktVyrobky.CheckedItems.Count() > 0 And Me.cisloExistuje = False Then
            My.Forms.fZadaniPrijmyPolozkyDialog.ShowDialog()
        Else
            MsgBox("Je třeba vyplnit následující položky: " & vbCrLf & fill, MsgBoxStyle.Exclamation, "Chyba!")
        End If
    End Sub


    Private Sub btnZrusit_Click(sender As System.Object, e As System.EventArgs) Handles btnZrusit.Click
        My.Forms.fMain.lblTStrip1.Text = ZRUSENO
        Me.btnZrusit.Focus()
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
        Me.Close()
    End Sub

    Private Sub tboxCisloFaktury_TextChanged(sender As Object, e As System.EventArgs) Handles tboxCisloFaktury.TextChanged
        If Me.tboxCisloFaktury.Text.Length > 0 And Me.tboxCisloFaktury.BackColor = Color.Red Then
            Me.tboxCisloFaktury.BackColor = System.Drawing.Color.White
        End If
        'checks no. of invoice, whether is it already in dtb
        If Me.cboxZpusobPlatby.SelectedIndex <> 1 Then
            Dim pocetRadku As Long = Me.DtbDataSet1.seznam_PrijemFaktury.Rows.Count()
            Dim cisloFaktury As String = Trim(Me.tboxCisloFaktury.Text)
            If pocetRadku > 0 Then
                For i As Long = 0 To pocetRadku - 1
                    If cisloFaktury = Me.DtbDataSet1.seznam_PrijemFaktury.Rows.Item(i).Item(1) Then
                        MsgBox("Faktura pod číslem " & cisloFaktury & " již v databázi existuje!" & vbCrLf _
                               & "Zadejte, prosím, jiné číslo faktury.", _
                               MsgBoxStyle.Critical, "Chyba!")
                        Me.cisloExistuje = True
                        Me.tboxCisloFaktury.Text = ""
                        Exit For
                    Else
                        Me.cisloExistuje = False
                    End If
                Next i
            End If
        End If
    End Sub

    Private Sub tboxAdresa_LostFocus(sender As Object, e As System.EventArgs) Handles tboxAdresa.LostFocus
        If Me.tboxAdresa.Text.Length > 0 And Me.tboxAdresa.BackColor = Color.Red Then
            Me.tboxAdresa.BackColor = System.Drawing.Color.White
        End If
    End Sub

    Private Sub tboxJmeno_LostFocus(sender As Object, e As System.EventArgs)
        If Me.tboxJmeno.Text.Length > 0 And Me.tboxJmeno.BackColor = Color.Red Then
            Me.tboxJmeno.BackColor = System.Drawing.Color.White
        End If
    End Sub

    Private Sub tboxPostovne_LostFocus(sender As Object, e As System.EventArgs) Handles tboxPostovne.LostFocus
        If Me.tboxPostovne.Text.Length > 0 And Me.tboxPostovne.BackColor = Color.Red Then
            Me.tboxPostovne.BackColor = System.Drawing.Color.White
        End If
        'If Me.tboxPostovne.Text.Length > 0 And Me.tboxPostovne.Text.Contains(",") = True Then
        '    Me.tboxPostovne.Text = Me.tboxPostovne.Text.Replace(",", ".")
        'End If
    End Sub

    Private Sub tboxPrijmeni_LostFocus(sender As Object, e As System.EventArgs) Handles tboxPrijmeni.LostFocus
        If Me.tboxPrijmeni.Text.Length > 0 And Me.tboxPrijmeni.BackColor = Color.Red Then
            Me.tboxPrijmeni.BackColor = System.Drawing.Color.White
        End If
    End Sub

    Private Sub chboxFaktVyrobky_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles chboxFaktVyrobky.SelectedIndexChanged
        If Me.chboxFaktVyrobky.CheckedItems.Count > 0 And Me.chboxFaktVyrobky.BackColor = Color.Red Then
            Me.chboxFaktVyrobky.BackColor = System.Drawing.Color.White
        End If
    End Sub

    Private Sub btnPridatVyrobek_Click(sender As System.Object, e As System.EventArgs) Handles btnPridatVyrobek.Click
        Me.btnPridatVyrobek.DialogResult = Windows.Forms.DialogResult.Yes
        My.Forms.fSetVyrobkyDialog.ShowDialog()
    End Sub

    Private Sub tboxPostovne_TextChanged(sender As Object, e As System.EventArgs) Handles tboxPostovne.TextChanged
        If tboxPostovne.Text.Length > 0 And cboxZpusobPlatby.SelectedIndex <> 1 Then
            If Decimal.TryParse(tboxPostovne.Text.Trim, Decimal.One) = False Then
                If tboxPostovne.Text.Contains(",") = True Then
                    tboxPostovne.Text = tboxPostovne.Text.Replace(",", ".")
                    tboxPostovne.SelectionStart = tboxPostovne.Text.Length
                    tboxPostovne.Select()
                    If Decimal.TryParse(tboxPostovne.Text, Decimal.One) = False Then
                        MsgBox("Zadaná hodnota není desetinné číslo ve správném tvaru!", MsgBoxStyle.Critical, "Chyba!")
                        Exit Sub
                    End If
                Else
                    MsgBox("Zadaná hodnota není desetinné číslo ve správném tvaru!", MsgBoxStyle.Critical, "Chyba!")
                End If
            End If
        End If
    End Sub


    Public Sub tboxJmeno_TextChanged(sender As Object, e As System.EventArgs) Handles tboxJmeno.TextChanged
        Dim tb As TextBox = tboxJmeno
        Dim t As String = tb.Text
        Dim tab As DataTable = Me.DtbDataSet1.seznam_odberatele
        Dim pR As Long = tab.Rows.Count()
        Dim cV As Long = 0

        Erase arrRec

        If t.Length > 0 Then
            If pR > 0 Then
                For i As Long = 0 To pR - 1
                    Dim row As DataRow = tab.Rows.Item(i)
                    If row.Item("jmeno").ToString.StartsWith(t, StringComparison.CurrentCultureIgnoreCase) = True _
                        Or row.Item("jmeno").ToString = t.Trim Then
                        cV += 1
                    End If
                Next i
                '---
                If cV > 0 Then
                    ReDim arrRec(cV - 1, 2)
                    Dim x As Long = 0

                    For i As Long = 0 To arrRec.GetUpperBound(0)
                        For j As Long = x To pR - 1
                            Dim row As DataRow = tab.Rows.Item(j)
                            If row.Item("jmeno").ToString.StartsWith(t, StringComparison.CurrentCultureIgnoreCase) = True _
                                Or row.Item("jmeno").ToString = t.Trim Then
                                arrRec(i, 0) = row.Item("jmeno")
                                arrRec(i, 1) = row.Item("prijmeni")
                                arrRec(i, 2) = row.Item("adresa")
                                x = j + 1
                                Exit For
                            Else
                                x = j + 1
                            End If
                        Next j
                    Next i
                    'ttipJmeno.SetToolTip(tb, cV.ToString.ToUpper)
                    ttipJmeno.Show(cV.ToString & vbCrLf & "Klepněte na tlačítko 'Zobrazit'", Me.tboxJmeno, 5000)
                    btnShowWhisprd.Visible = True
                Else
                    ttipJmeno.Hide(Me.tboxJmeno)
                    btnShowWhisprd.Visible = False
                End If
            End If
        Else
            ttipJmeno.Hide(Me.tboxJmeno)
            btnShowWhisprd.Visible = False
            If tboxPrijmeni.Text.Trim.Length > 0 Then
                tboxPrijmeni.Text = ""
            End If
            If tboxAdresa.Text.Trim.Length > 0 Then
                tboxAdresa.Text = ""
            End If
        End If

    End Sub

    Private Sub btnShowWhisprd_Click(sender As System.Object, e As System.EventArgs) Handles btnShowWhisprd.Click
        My.Forms.fZadaniWhispedList.ShowDialog()
    End Sub
End Class